{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "threaded-helena",
   "metadata": {},
   "source": [
    "## 导入依赖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "numeric-providence",
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "import numpy as np\n",
    "# 阿凯机器人工具箱\n",
    "from kyle_robot_toolbox.robot_arm.arm5dof_uservo import Arm5DoFUServo"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "excellent-canadian",
   "metadata": {},
   "source": [
    "## 创建机械臂"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "alien-nickel",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建机械臂\n",
    "arm = Arm5DoFUServo(config_folder=\"./config\", \\\n",
    "                    is_init_pose=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43c1da7b",
   "metadata": {},
   "source": [
    "## 末端控制(自动生成Pitch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "203c2e17",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_tool = [150.0, 80.0, 200.0]\n",
    "arm.set_tool_pose(p_tool)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b3776d86",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_tool = [200.0, -40.0, 100.0]\n",
    "arm.set_tool_pose(p_tool)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "76c9cee9",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_tool = [140.0, 0.0, 150.0]\n",
    "tool_roll = np.radians(-135.0)\n",
    "arm.set_tool_pose(p_tool, tool_roll=tool_roll)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "306152c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_tool = [180.0, -50.0, 100.0]\n",
    "tool_roll = np.radians(90.0)\n",
    "arm.set_tool_pose(p_tool, tool_roll=tool_roll)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "sorted-religious",
   "metadata": {},
   "source": [
    "## 末端控制(设定Pitch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "15448b81",
   "metadata": {},
   "outputs": [],
   "source": [
    "tool_posi = [180.0, -50.0, 100.0]\n",
    "tool_pitch = np.radians(110.0)\n",
    "tool_roll = np.radians(180.0)\n",
    "arm.set_tool_pose(tool_posi, tool_pitch=tool_pitch, tool_roll=tool_roll)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38a4ed5d",
   "metadata": {},
   "source": [
    "## 获取当前末端位姿"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9e43cff4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "坐标: X 179.5  Y -46.0 Z 87.7\n",
      "俯仰角(Pitch) 114.0 deg\n",
      "横滚角(Roll) -150.0 deg\n"
     ]
    }
   ],
   "source": [
    "# 返回的数据格式: [x6, y6, z6, pitch, roll]  \n",
    "x, y, z, pitch, roll = arm.get_tool_pose()\n",
    "print(f\"坐标: X {x:.1f}  Y {y:.1f} Z {z:.1f}\")\n",
    "print(f\"俯仰角(Pitch) {np.degrees(pitch):.1f} deg\")\n",
    "print(f\"横滚角(Roll) {np.degrees(roll):.1f} deg\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
